package com.ehotting.edsta.mqbd.monitor.task.active.module;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.core.utils.SpringUtils;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.edsta.mqbd.signal.api.bean.MqSignalTcpData;
import com.ehotting.edsta.mqbd.signal.api.domain.netty.TaskSizeRequest;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.bean.MQBaseData;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.api.enums.ServerAvailableStateEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.logger.LOGGER_KEY;
import com.ehotting.mqbd.core.logger.MQSendLogger;
import com.ehotting.mqbd.core.logger.ModuleActiveLogger;
import com.ehotting.mqbd.core.monitor.ModuleActiveAble;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.env.Environment;

import java.util.HashMap;
import java.util.Map;

public class SignalAnalysisActive extends AbstractModuleActive {

    private static final Logger logger = LoggerFactory.getLogger(SignalAnalysisActive.class);

    private ModuleActiveLogger mqActiveLogger;

    public SignalAnalysisActive(ModuleActiveLogger mqActiveLogger) {
        this.mqActiveLogger = mqActiveLogger;
    }

    @Override
    public String getModule() {
        return SysModuleEnum.SIGNAL_ANALYSIS.getCode();
    }

    @Override
    public Integer doAction(SolverStateBean solverState) {
        logger.info("Start SignalAnalysisActive");

        if(solverState.getAvailableState()== ServerAvailableStateEnum.NOT){
              return super.doTelnet(solverState.getSolverInfo());
        }

        MqApplicationContext mqApplicationContext = new MqApplicationContext();
        mqApplicationContext.setModule(getModule());
        mqApplicationContext.setLocalHost(MQDBConstants.MONITOR_HOST);
        mqApplicationContext.setConsumerHost(MQDBConstants.DISPATCHER_HOST);

        AgentLinkManager agentLinkManager = SpringUtils.getBean(AgentLinkManager.class);
        String agentId = agentLinkManager.genAgentId(getModule(),0L,"0");

        agentLinkManager.setClientIpAndAgentId(agentId,mqApplicationContext.getLocalHost());

        MQBaseData data = MqSignalTcpData.create(TopicEnum.CONCURRENCY,
                mqApplicationContext,
                agentId,
                "");
        data.setSolverIp(solverState.getSolverInfo().getIp());
        data.setSync(false);

        TaskSizeRequest request = new TaskSizeRequest();
        data.setRequest(request);

        MQService mqService = SpringUtils.getBean(MQService.class);

        MqSignalTcpData mqSignalTcpData = (MqSignalTcpData)mqService.request(data,10);
        logger.info("JSON.toJSONString(mqSignalTcpData):{}",JSON.toJSONString(mqSignalTcpData));

        int taskSize = -1;
        TaskSizeRequest response =(TaskSizeRequest) mqSignalTcpData.getResponse();
        if(response != null){
            taskSize = response.getSize();
        }

        logger.info("taskSize:{}",taskSize);
        logger.info("Finish SignalAnalysisActive");
        return taskSize;
    }

    @Override
    public ModuleActiveLogger getModuleActiveLogger() {
        return mqActiveLogger;
    }

}